﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Infrastructure.Helpers.PwdHelper
{

    /// <summary>
    /// 计算密码强度
    /// </summary>
    public class PwdCheck
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static ChkRslt PasswordStrength(string password)
        {
            if (password.Length < 8 || password.Length > 16)
            {
                return new ChkRslt() { Pass = false, Msg = "密码长度不符合，密码长度：8-16" };  //纯符号密码
            }

            Regex rgx = new Regex(@"^[0-9a-zA-Z\x21-\x7e]{8,16}$");
            if (!rgx.IsMatch(password))
            {
                return new ChkRslt() { Pass = false, Msg = "密码只能包含数字，字母和字符" };  //纯符号密码
            }

            //字符统计
            int iNum = 0, iLtt = 0, iSym = 0;
            foreach (char c in password)
            {
                if (c >= '0' && c <= '9') iNum++;
                else if (c >= 'a' && c <= 'z') iLtt++;
                else if (c >= 'A' && c <= 'Z') iLtt++;
                else iSym++;
            }

            if (iLtt == 0 && iSym == 0)
            {
                return new ChkRslt() { Pass = false, Msg = "纯数字密码，请加入字符和字母" };  //纯符号密码
            }

            if (iNum == 0 && iLtt == 0)
            {
                return new ChkRslt() { Pass = false, Msg = "纯符号密码，请加入数字和字母" };  //纯符号密码
            }

            if (iNum == 0 && iSym == 0)
            {
                return new ChkRslt() { Pass = false, Msg = "纯字母密码，请加入字符和数字" }; //纯字母密码

            }
            if (iLtt == 0)
            {
                return new ChkRslt() { Pass = false, Msg = "数字和符号构成的密码，请加入字母" }; ; //数字和符号构成的密码
            }

            if (iSym == 0)
            {
                return new ChkRslt() { Pass = false, Msg = "数字和字母构成的密码，请加入字符" }; ; //数字和字母构成的密码
            }

            if (iNum == 0)
            {
                return new ChkRslt() { Pass = false, Msg = "字母和符号构成的密码，请加入数字" }; //字母和符号构成的密码
            }

            return new ChkRslt() { Pass = true, Msg = "密码符合" };
        }
    }


    /// <summary>
    /// 
    /// </summary>
    public class ChkRslt
    {
        /// <summary>
        /// 是否通过检测
        /// </summary>
        public bool Pass { set; get; }

        /// <summary>
        /// 检测结果
        /// </summary>
        public string Msg { set; get; }
    }

}
